ЛАБОРАТОРНАЯ РАБОТА № 4
«Сложные табличные вычисления в
JAVA»

Исходный код примера: 

package tsn.javase.lab04;
import java.math.BigDecimal;
import javax.swing.JOptionPane;
public class Form1 extends javax.swing.JFrame {
    public Form1() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    //                          
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Сложные табличные вычисления в JAVA");
setIconImage(java.awt.Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png")));
        setResizable(false);
        getContentPane().setLayout(null);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "K(i)", "Y(i)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                true, false
            };
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 0, 140, 188);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tsn/javase/lab04/lab.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(140, 0, 284, 104);
        jButton1.setText("Заполнить случайными");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(151, 120, 160, 23);
        jButton2.setText("Выполнить задание");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(150, 150, 160, 23);
        jButton3.setText("Выход");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(320, 150, 90, 23);
        jButton4.setText("Очистка");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4);
        jButton4.setBounds(320, 120, 90, 23);
        setSize(new java.awt.Dimension(438, 225));
        setLocationRelativeTo(null);
    }//                        
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Случайные числа
        jButton4ActionPerformed(null);
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            jTable1.setValueAt(Math.round(Math.random() * 100), i, 0);
        }
    }                                       
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Выполнение задания
        double fk = 1, sum = 0, pr = 1, y = 0;
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            if (jTable1.getValueAt(i, 0) == null) {
                JOptionPane.showMessageDialog(rootPane, "Проверьте правильностью заполнения столбца К(i)", "Ошибка ввода", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            sum += Integer.parseInt(jTable1.getValueAt(i, 0).toString()); // Расчет суммы
            try {
                pr *= Integer.parseInt(jTable1.getValueAt(i, 0).toString()); // Расчет произведения
                fk *= i + 1; // Расчет факториала
                y = (Math.sqrt((pr - sum) / fk) / (i + 1)) * (Math.pow(Math.tan((i + 1) / 2), 2)); // Расчет по заданной формуле
                jTable1.setValueAt(
                        BigDecimal.valueOf(y).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue(), i, 1); // Вывод результата
            } catch (Exception e) {
                jTable1.setValueAt(0, i, 1);
            }
        }
    }                                       
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Выход из программы
        System.exit(0);
    }                                       
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Очистка таблицы
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            for (int j = 0; j < jTable1.getColumnCount(); j++) {
                jTable1.setValueAt(null, i, j); // очень интересный момент, если тут поставить пустую строку в отчистку,
            }                                             //  тогда не будет срабатывать защита, так как она работает через null
        }
    }                                        
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                  
}